
const app = getApp()
// var cityData3 = require('https://zbz-imgs.oss-cn-beijing.aliyuncs.com/city.js')
var cityData3 = require('../../../utils/city.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        info: {
            addressDetail: "",
            addressId: '',
            userName: "",
            userTel: "",
            userId: app.globalData.userId
        },
        cityData3: [],
        show: false,
        cascaderValue: '',
        addressStr: '',
        morenFlag: false,
    },


    addHandle() {
        var info = this.data.info,
            addressStr = this.data.addressStr,
            url = app.globalData.url + "user/addUserAddr",
            that = this;
        if (info.userName == '') {
            wx.showToast({
                title: '请输入收件人',
                icon: 'none'
            })
            return
        }
        if (info.userTel == '') {
            wx.showToast({
                title: '请输入联系电话',
                icon: 'none'
            })
            return
        }
        if (addressStr == '') {
            wx.showToast({
                title: '请选择所在地区',
                icon: 'none'
            })
            return
        }
        if (info.addressDetail == '') {
            wx.showToast({
                title: '请输入详细地址',
                icon: 'none'
            })
            return
        }



        console.log(info);
        info.userAddr = addressStr + ' ' + info.addressDetail
        console.log(info);

        delete info.addressDetail
        info.userId = app.globalData.userId

        info.flag = info.addressId == '' ? 0 : 1

        console.log(info);

        wx.showLoading({
            title: '',
        })
        app.wxRequest(url, info, res => {
            console.log(res);
            if (res.flag) {
                wx.hideLoading()
                // 修改本地存储的地址 更新本地
                if (wx.getStorageSync('address')) {
                    var address = JSON.parse(wx.getStorageSync('address'))
                    if (that.data.info.addressId != '' && address.addressId == that.data.info.addressId) {
                        wx.setStorageSync('address', JSON.stringify(that.data.info))
                    }
                }


                wx.showModal({
                    title: '提示',
                    content: '地址已保存',
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.navigateBack()
                        }
                    }
                })
            }
        },
            err => {
                console.log(err);
            })
    },



    delHandle() {
        var that = this;
        wx.showModal({
            title: '提示',
            content: '是否要删除该地址',
            complete: (res) => {
                if (res.cancel) {

                }
                if (res.confirm) {
                    var url = app.globalData.url + "user/delUserAddr";
                    wx.showLoading({
                        title: '',
                    })
                    app.wxRequest(url, {
                        addressId: that.data.info.addressId,
                    }, res => {
                        console.log(res);
                        if (res.flag) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '地址删除成功',
                                showCancel: false,
                                complete: (res) => {
                                    if (res.confirm) {
                                        if (wx.getStorageSync('address')) {
                                            var address = JSON.parse(wx.getStorageSync('address'))
                                            if (address.addressId == that.data.info.addressId) {
                                                wx.removeStorageSync('address')
                                            }
                                        }

                                        wx.navigateBack()
                                    }
                                }
                            })
                        }
                    },
                        err => {
                            console.log(err);
                        })
                }
            }
        })

    },


    onFlagChange(e) {
        console.log(e);
        var info = this.data.info;
        console.log(info);
        if (info.addressId != '' && e.detail) {
            this.setHandle()
        }
        this.setData({
            morenFlag: e.detail
        })
    },
    onFinish(e) {
        console.log(e);
        var arr = e.detail.selectedOptions;
        this.setData({
            addressStr: arr[0].text + arr[1].text + arr[2].text,
            show: false
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    showHandle() {
        this.setData({
            show: true
        })
    },
    // 输入框编辑
    inputHandle(e) {
        var str = e.currentTarget.dataset.flag,
            val = e.detail.value;
        this.setData({
            [str]: val,
        })
        console.log(this.data);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        console.log(cityData3);
        if (options.obj) {
            var obj = JSON.parse(options.obj);
            console.log(obj);
            var info = {
                userName: obj.userName,
                userTel: obj.userTel,
                addressId: obj.addressId,
                userAddr: obj.userAddr,
                addressDetail: obj.userAddr.split(' ')[1],
            }
            this.setData({
                addressStr: obj.userAddr.split(' ')[0],
                info
            })
        }

        this.setData({
            cityData3: cityData3.cityData3,
        })
        console.log(this.data);

    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})